tibco.admin.Main.loadResource("tibco/admin/deployment/sa/svcList/jss/jss.xml", "tibco.admin", "jss48", "ljss", false);
tibco.admin.Main.loadResource("tibco/admin/deployment/sa/svcList/jss/xslt.xml", "tibco.admin", "xslt48", "jss", false);

/**
 **/
jsx3.lang.Package.definePackage("tibco.admin.deployment.sa.svcList", function (svcListp) {
});

/**
 **/
jsx3.lang.Class.defineClass("tibco.admin.deployment.sa.svcList.SvcList",
        jsx3.lang.Object,
        [tibco.admin.deployment.sa.ServiceAssemblyInterface],
        function(svcList) {
            // handler for this project and GUI interface.
            svcList.prototype.rootNode = null;

            // this property used for save modify state.
            svcList.prototype.hasModified = false;

            // this property used for save present mode.
            svcList.prototype.visionMode = "view";

            // this property used for save service assembly id.
            svcList.prototype.saEntityId = null;

            svcList.prototype.getButtonText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@buttonText_48");
            }

            svcList.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            /**
             * Called when destroy event fired.
             */
            svcList.prototype.destroyPlugin = function() {
            }

            svcList.prototype.onDestroy = function() {
                this.unsubscribeAll();
                this.rootNode = null;
            }

            svcList.getInstance = function() {
                if(!this.instance) {
                    this.instance = new tibco.admin.deployment.sa.svcList.SvcList();
                }
                return this.instance;
            }

            svcList.prototype.onAfterComponentUnloaded = function() {
            }

            svcList.prototype.loadComponent = function(parent) {
                var me = this;
                tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/deployment/sa/svcList/ui/svcList.xml", me, me.componentLoaded);
            }

            svcList.prototype.componentLoaded = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.rootNode = objEvent.obj;
                    this.rootNode.handler = this;
                    this.setVisionMode(this.visionMode);
                    this.subscribeAll()
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            /**
             * Subscribe all related events.
             * This function will called automaticly by parenet component.
             */
            svcList.prototype.subscribeAll = function() {
                var me = this;
                // subscribe service assembly selected event message.
                tibco.admin.PubSub.subscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED,
                        me, me.onServiceAssemblySelected);
            };

            /**
             * Unsubscribe events which have already subscribed.
             * This function will called automaticly by parenet component.
             */
            svcList.prototype.unsubscribeAll = function() {
                tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED,
                        this);
            };

            /**
             * this function be setted as a subscribe callback function.
             * Called after a service assembly item selected.
             */
            svcList.prototype.onServiceAssemblySelected = function(objEvent) {
                if(this.isInEditMode()) {
                    return;
                }
                if(!objEvent.entityID) return false;
                this.rootNode.getDescendantOfName("mtxServices_48").clearXmlData();
                this.rootNode.getDescendantOfName("mtxServices_48").repaint(true);

                this.saEntityId = objEvent.entityID;
                // get services list when servcie assembly is unempty.
                if(this.saEntityId)
                {
                    this.callgetServicesForServiceAssembly();
                }
            };

            /**
             * method used for set UI vision mode
             * mode {string} the value specified what type of vision you want to setting.
             */
            svcList.prototype.setVisionMode = function(mode) {
                this.visionMode = mode;
                if(this.visionMode == "edit")
                {
                    // reset modified status
                    this.hasModified = false;
                }

                this.rootNode.getDescendantOfName("btnEdit_48").setDisplay(mode == "edit" ? jsx3.gui.Block.DISPLAYNONE : jsx3.gui.Block.DISPLAYBLOCK, true);
                this.rootNode.getDescendantOfName("btnCancel_48").setDisplay(mode == "edit" ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE, true);
                this.rootNode.getDescendantOfName("btnSave_48").setDisplay(mode == "edit" ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE, true);
            };

            /**
             * Used for make present to "edit" mode.
             * Called on "Edit" button clicked.
             */
            svcList.prototype.onEdit = function() {
                this.setVisionMode("edit");
                this.beginEditMode();
            };

            /**
             * Called when user click "Cancel" button.
             */
            svcList.prototype.onCancel = function() {
                // if edit nothing, just return.
                if(!this.hasModified)
                {
                    this.setVisionMode("view");
                    this.endEditMode();
                    return;
                }

                // popup up a confirm dialog, when user click the cancel button.
                var me = this;
                tibco.admin.Main.confirm(tibco.admin.Main.getServer().getDynamicProperty("@@confirmTitle_48"),
                        tibco.admin.Main.getServer().getDynamicProperty("@@discardEdit_48"),
                        function(confirmationDialog)
                        {
                            me.setVisionMode("view");
                            me.endEditMode();
                            // restore data list to the view model.
                            me.callgetServicesForServiceAssembly();
                            confirmationDialog.doClose();
                        },
                        function(confirmationDialog)
                        {
                            confirmationDialog.doClose();
                        }, tibco.admin.Main.getServer().getDynamicProperty("@@btnConfirmYes_48"), tibco.admin.Main.getServer().getDynamicProperty("@@btnConfirmNo_48"), 2, null, null, tibco.admin.Main.getServer().getDynamicProperty("@@btnConfirmTitle_48"));
            };

            /**
             * Called when user click Save button
             */
            svcList.prototype.onSave = function() {
                this.callupdateServicesForServiceAssembly();
            };

            /**
             * Called when user change any textbox-content of "Service Queue" in matrix cell.
             */
            svcList.prototype.txtboxServiceQueue_48_OnChange = function() {
                this.hasModified = true;
            };

            /**
             * Call server backend to get services for current selected service assembly.
             */
            svcList.prototype.callgetServicesForServiceAssembly = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/svcList/rules/getServicesForServiceAssembly.xml",
                        "getServicesForServiceAssembly");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
                objService.saEntityId = this.saEntityId;

                var me = this;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetServicesForServiceAssemblySuccess);
                objService.doCall();
            };

            svcList.prototype.ongetServicesForServiceAssemblySuccess = function(objEvent) {
                this.rootNode.getDescendantOfName("mtxServices_48").repaint();
            };

            svcList.prototype.ongetServicesForServiceAssemblyError = function(objEvent) {
            };

            svcList.prototype.ongetServicesForServiceAssemblyInvalid = function(objEvent) {
            };

            /**
             * Call server backend to update services for current selected service assembly.
             */
            svcList.prototype.callupdateServicesForServiceAssembly = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/deployment/sa/svcList/rules/updateServicesForServiceAssembly.xml",
                        "updateServicesForServiceAssembly");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
                objService.saEntityId = this.saEntityId;

                var me = this;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onupdateServicesForServiceAssemblySuccess);
                objService.doCall();
            };

            svcList.prototype.onupdateServicesForServiceAssemblySuccess = function(objEvent) {
                this.setVisionMode("view");
                this.endEditMode();
                this.callgetServicesForServiceAssembly();
            };

            svcList.prototype.onupdateServicesForServiceAssemblyError = function(objEvent) {
            };

            svcList.prototype.onupdateServicesForServiceAssemblyInvalid = function(objEvent) {
            };

            svcList.prototype.getComponentPath = function() {
                return  "tibco/admin/deployment/sa/svcList/component.xml"
            }
        });
